package com.todo.event.activity.infrastructure.repository.impl;

import com.todo.common.utils.StringUtils;
import com.todo.event.activity.common.enums.ExecuteRecordStatusEnum;
import com.todo.event.activity.infrastructure.entity.po.EventActivityRecordPO;
import com.todo.event.activity.infrastructure.mapper.EventActivityRecordMapper;
import com.todo.event.activity.infrastructure.repository.EventActivityRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/15 14:28
 * @description
 */
@Slf4j
@Repository
public class EventActivityRecordRepositoryImpl implements EventActivityRecordRepository {

    @Resource
    private EventActivityRecordMapper eventActivityRecordMapper;


    @Override
    public Long insertActivityRecord(EventActivityRecordPO eventActivityRecordPO) {
        if (eventActivityRecordPO == null) {
            return null;
        }
        if (eventActivityRecordPO.getId() == null || eventActivityRecordPO.getActivityId() == null || eventActivityRecordPO.getUserId() == null || eventActivityRecordPO.getUk() == null) {
            return null;
        }
        if (eventActivityRecordPO.getOperateTime() == null || eventActivityRecordPO.getOperateTime().after(new Date())) {
            return null;
        }
        eventActivityRecordPO.setRetryCount(0);
        eventActivityRecordPO.setStatus(ExecuteRecordStatusEnum.EXECUTING.getType());
        return eventActivityRecordMapper.insertEventActivityRecord(eventActivityRecordPO) > 0 ? eventActivityRecordPO.getId() : null;
    }

    @Override
    public Boolean updateActivityRecordStatus(Long id, ExecuteRecordStatusEnum oldStatus, ExecuteRecordStatusEnum newStatus) {
        if (id == null || oldStatus == null || newStatus == null) {
            return false;
        }
        return eventActivityRecordMapper.updateActivityRecordStatus(id, oldStatus.getType(), newStatus.getType()) > 0;
    }

    @Override
    public EventActivityRecordPO queryRecordByActivityIdAndUK(Long activityId, String uk) {
        if (activityId == null || StringUtils.isBlank(uk)) {
            return null;
        }
        return eventActivityRecordMapper.queryRecordByUK(activityId, uk);
    }

    @Override
    public List<Long> queryStatusRecordIdListByTime(ExecuteRecordStatusEnum status, Date startTime, Date endTime) {
        if (status == null || startTime == null || endTime == null) {
            return Collections.emptyList();
        }
        if (startTime.after(endTime) || startTime.after(new Date())) {
            return Collections.emptyList();
        }
        return eventActivityRecordMapper.queryStatusRecordIdListByTime(status.getType(), startTime, endTime);
    }

    @Override
    public List<EventActivityRecordPO> queryRecordListByIdList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        return eventActivityRecordMapper.queryRecordListByIdList(idList);
    }

    @Override
    public boolean updateActivityRecordBaseInfo(Long recordId, Integer retryCount, ExecuteRecordStatusEnum recordStatus) {
        if (recordId == null){
            return false;
        }
        return eventActivityRecordMapper.updateActivityRecordBaseInfo(recordId, retryCount,recordStatus == null ? null : recordStatus.getType()) > 0;
    }
}
